<template>
  <div class="login">
    <div class="el-login-biaoti">
      <div class="biaoti-content">
        <div>
          <img src="@/assets/images/HomeLogo.png">
        </div>
        <div>
          <div class="biaoti-samll">在线交通信息一体化平台</div>
          <div class="biaoti-big">新区交通综合信息服务系统</div>
<!--          <div class="biaoti-samll">www.wangzhi.gov.com</div>-->
        </div>
      </div>
    </div>
    <div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">企业用户登录</h3>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="统一信用代码"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item label="滑块验证：">
          <!-- 滑动验证组件 通过调用@sliding方法得到滑动状态-->
          <div><slider ref="slider" @sliding="slidStatus" /></div>
        </el-form-item>
        <!--      <el-form-item v-if="captchaEnabled" prop="code">-->
        <!--        <el-input-->
        <!--          v-model="loginForm.code"-->
        <!--          auto-complete="off"-->
        <!--          placeholder="验证码"-->
        <!--          style="width: 63%"-->
        <!--          @keyup.enter.native="handleLogin"-->
        <!--        >-->
        <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
        <!--        </el-input>-->
        <!--        <div class="login-code">-->
        <!--          <img :src="codeUrl" class="login-code-img" @click="getCode">-->
        <!--        </div>-->
        <!--      </el-form-item>-->
        <el-checkbox v-model="loginForm.holdLogin" style="margin:0px 10px 25px 0px;">30天内自动登录</el-checkbox>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <div v-if="register" style="float: right;">
          <router-link class="link-type" :to="'/forgotPassword'">忘记密码</router-link>
        </div>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div v-if="register" style="float: right;">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <!--    <div class="el-login-footer">-->
    <!--      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>-->
    <!--    </div>-->
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import slider from './slider'

export default {
  name: 'Login',
  components: {
    slider
  },
  data() {
    return {
      codeUrl: '',
      loginForm: {
        // 滑动校验是否通过
        canLogin: false,
        // 是否保持登录30天
        holdLogin: false,
        username: '',
        password: '',
        rememberMe: false,
        // code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ]
        // code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    // 滑动验证时调用的方法
    slidStatus(data) {
      console.log('data', data)
      this.loginForm.canLogin = data.mouseMoveState
    },
    // 校验滑动验证的方法
    toSubmit() {
      if (this.$refs['slider']) {
        // 未通过验证时，提示错误信息并返回
        if (!this.$refs['slider'].confirmSuccess) {
          this.$message.error('请拖动滑块验证')
          return
        }
      }
      // 通过验证后提交
      // TO DO ...
      this.$message.success('验证成功，可提交')
    },
    getCode() {
      getCodeImg().then(res => {
        console.log('res', res)
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      const holdLogin = Cookies.get('holdLogin')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        canLogin: this.loginForm.canLogin,
        holdLogin: holdLogin !== 'false'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
            Cookies.set('holdLogin', this.loginForm.holdLogin, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
            Cookies.remove('holdLogin')
          }
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: right;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/Loginbanner.png");
  background-size: cover;
  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    margin-right: 200px;
    .title {
      margin: 0px auto 30px auto;
      text-align: center;
      color: #707070;
    }
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
//.login-code {
//  width: 33%;
//  height: 38px;
//  float: right;
//  img {
//    cursor: pointer;
//    vertical-align: middle;
//  }
//}
.el-login-biaoti {
  height: 40px;
  line-height: 40px;
  position: fixed;
  top: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  .biaoti-content {
    padding: 0px 200px;
    width: 100%;
    background-color: #ffffff;
    display: flex;
    flex-direction: row;
    justify-content: left;
    align-items: flex-start;
    img {
      width: 100px; height: 120px
    }
    .biaoti-samll {
      color: #333333;
      font-size: 14px;
      text-align: left;
    }
    .biaoti-big {
      color: #333333;
      font-weight: bold;
      font-size: 32px;
      text-align: left;
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
